<?php

namespace api\modules\v1\models;

use api\common\models\User;
use Yii;
use api\common\models\Common;
use yii\db\Expression;
use yii\web\HttpException;
use \yii\db\Query;
use yii\web\NotFoundHttpException;
class Report extends Common
{

    /**
     * @return array|string
     */
    public static function tableName()
    {
        return '{{%report_app}}';
    }


    public function rules()
    {
        return [
            ['receive_uid', 'required','message'=>'被举报人不能为空'],
            ['receive_uid', 'integer','message'=>'被举报人必须是整数'],
            ['receive_uid', 'validateTypeUid'],

            ['reason', 'required','message'=>'举报理由不能为空'],
            ['reason', 'validateReason'],

            ['reason_other', 'string', 'max' => 50,'message'=>'其他举报理由最长为50个字符'],

            ['catid', 'required','message'=>'栏目不能为空'],
            ['catid', 'integer','message'=>'栏目必须是整数'],
            ['catid', 'default', 'value' => 0],

            ['news_id', 'required','message'=>'文章id不能为空'],
            ['news_id', 'integer','message'=>'文章id必须是整数'],
            ['news_id', 'validateNewsId'],

            ['title', 'required'],
            ['url', 'required'],
            ['url', 'url'],

            ['status', 'default', 'value' => 0],
        ];
    }

    public function attributeLabels()
    {
        return [
            'report_num' => Yii::t('app', '单号'),
            'report_uid' => Yii::t('app', '举报人'),
            'receive_uid' => Yii::t('app', '被举报人'),
            'reason' => Yii::t('app', '举报理由'),
            'reason_other' => Yii::t('app', '其他举报理由'),
            'news_id' => Yii::t('app', '文章'),
            'catid' => Yii::t('app', '栏目'),
            'status' => Yii::t('app', '状态'),
            'title' => Yii::t('app', '标题'),
            'url' => Yii::t('app', '链接'),
        ];
    }

    public function validateNewsId($attribute, $params)
    {
        if(!Category::findOne(['catid'=>$this->catid])){
            $this->addError($this->catid,'栏目id:'.$this->catid.'不存在');
            return false;
        }

        //验证文章id是否存在
        $news_model = News::getTable($this->catid);
        $db = \Yii::$app->db;
        if (!$rows = $db->createCommand('SELECT id,catid,thirdid FROM {{%' . $news_model . '}} where id = ' . $this->news_id)->queryOne()) {
            $this->addError($this->news_id,'文章id:'.$this->news_id.'不存在');
            return false;
        }

        //验证 文章是否在栏目里面
        if($rows['catid'] != $this->catid){
            $this->addError($this->news_id,'文章id:'.$this->news_id.'与栏目id:'.$this->catid.'不匹配');
            return false;
        }

        //验证是否是第三方发布 且对比发布者id
        if($rows['thirdid'] == '' || $rows['thirdid'] != $this->receive_uid){
            $this->addError($this->receive_uid,'文章id:'.$this->news_id.'与举报用户id:'.$this->receive_uid.'不匹配');
            return false;
        }
        return true;
    }

    public function validateReason($attribute, $params){
        $reason = explode(',',$this->reason);
        foreach($reason as $value){
            if(!is_numeric($value)){
                $this->addError($this->reason,'举报理由数据错误!');
                return false;
            }
        }
    }

    public function validateTypeUid($attribute, $params){
        if(!User::findOne(['id'=>$this->receive_uid])){
            $this->addError($this->receive_uid,'举报的用户不存在');
            return false;
        }

        if($this->receive_uid == Yii::$app->user->identity->id){
            $this->addError($this->receive_uid,'不能举报自己');
            return false;
        }
        return true;
    }

    public function report(){
        if ($this->validate()) {
            if(self::findOne(['catid'=>$this->catid,'news_id'=>$this->news_id,'report_uid'=>Yii::$app->user->identity->id,'receive_uid'=>$this->receive_uid])){
                throw new NotFoundHttpException('请勿重复提交.');
            }

            if($this->reason == 99 && $this->reason_other == ''){
                throw new NotFoundHttpException('其他举报理由不能为空.');
            }

            $this->report_uid = Yii::$app->user->identity->id;
            $this->time = date('Y-m-d H:i:s');
            $this->report_num = 'JB'.date('YmdHis',time()).str_pad(Yii::$app->user->identity->id,7, "0", STR_PAD_LEFT);

            $this->reason = str_replace(' ','',$this->reason);

            if ($this->save(false)) {
                return true;
            }
        }
        return null;
    }
}
